package com.library;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletURL;
import javax.servlet.http.HttpServletRequest;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portlet.PortletURLFactoryUtil;
import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.slayer.model.LMSBook;
import com.slayer.service.LMSBookLocalServiceUtil;

/**
 * Portlet implementation class LibraryPortlet
 */
public class LibraryPortlet extends MVCPortlet {

	private static final Log log = LogFactoryUtil.getLog(LibraryPortlet.class);
	public void updateBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		
		/*start test code*/
		HttpServletRequest httpRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));
		log.info(httpRequest.getSession().getServletContext().getServletContextName());
		/*end test code*/
		
		ServiceContext context = null;
		try {
			context = ServiceContextFactory.getInstance(actionRequest);
		} catch (PortalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.debug("inside process action class");
		
		String portletNameSpace = actionResponse.getNamespace();
		log.debug("portal name space  " + portletNameSpace);
		String bookTitle = ParamUtil.getString(actionRequest, "bookTitle");
		String author = ParamUtil.getString(actionRequest, "author");
		log.debug("bookTitle  " + bookTitle);
		log.debug("author  " + author);
		String test = MyLibraryConstants.TEST_Constant;
		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		LMSBook book = null;
		if (bookId > 0l) {
			modifyBook(bookId, bookTitle, author, context);
		} else {
			book = LMSBookLocalServiceUtil.insertBook(bookTitle, author,
					context);
		}
		
		// insertBook(bookTitle, author);
		// PortletRequest prequest = (PortletRequest)actionRequest;
		// String redirectUrl =
		// ParamUtil.getString(actionRequest,"redirectUrl");
		PortletConfig pConfig = (PortletConfig) actionRequest
				.getAttribute("javax.portlet.config");
		PortletRequest prequest = (PortletRequest) actionRequest
				.getAttribute("javax.portlet.request");
		String portletName = pConfig.getPortletName();
		ThemeDisplay td = (ThemeDisplay) actionRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		// PortletURL sucessPageUrl = PortletURLFactoryUtil.create(prequest,
		// portletName+"_WAR_"+portletName+"portlet",td.getPlid(),
		// PortletRequest.RENDER_PHASE);
		PortletURL successPageURL = PortletURLFactoryUtil.create(prequest,
				portletName + "_WAR_" + portletName + "portlet", td.getPlid(),
				PortletRequest.RENDER_PHASE);
		successPageURL.setParameter("jspPage", MyLibraryConstants.SUCESS_PAGE);

		log.debug("sucessPageUrl  ||||||||>>>>>>>>>>  "
				+ successPageURL);
		// actionResponse.sendRedirect(successPageURL.toString());
		actionResponse.setRenderParameter("jspPage",
				MyLibraryConstants.SUCESS_PAGE);

	}

	private void modifyBook(long bookId, String bookTitle, String author,
			ServiceContext context) {
		LMSBook lmsBook = null;
		try {
			lmsBook = LMSBookLocalServiceUtil.fetchLMSBook(bookId);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (Validator.isNotNull(lmsBook)) {
			lmsBook.setAuthor(author);
			lmsBook.setBookTitle(bookTitle);
			lmsBook.setModifiedDate(new Date());
		}
		try {
			LMSBookLocalServiceUtil.updateLMSBook(lmsBook);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteBook(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		long bookId = ParamUtil.getLong(actionRequest, "bookId");
		String renderURL = ParamUtil.getString(actionRequest, "renderURL");
		log.debug("inside deleteBook   " + bookId);
		if (bookId > 0l) {
			try {
				LMSBookLocalServiceUtil.deleteLMSBook(bookId);
			} catch (PortalException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		actionResponse.setRenderParameter("jspPage",
				MyLibraryConstants.SHOW_LIST);
		// actionResponse.sendRedirect(renderURL.toString());
	}

	public void searchBooks(ActionRequest actionRequest,
			ActionResponse actionResponse) throws IOException, PortletException {
		List<LMSBook> lmsBook = null;
		String searchTerm = ParamUtil.getString(actionRequest, "searchTerm");
		if (Validator.isNull(searchTerm))
			return;

		try {
			lmsBook = LMSBookLocalServiceUtil.searchBooks(searchTerm);
			actionRequest.setAttribute("SEARCHRESULT", lmsBook);
			actionResponse.setRenderParameter("jspPage",
					MyLibraryConstants.SHOW_LIST);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void setPreferences(ActionRequest actionRequest,
            ActionResponse actionResponse) 
                    throws IOException, PortletException {

    String maxBooksLimit = 
                    ParamUtil.getString(actionRequest, "maxBooksLimit");
    
    PortletPreferences preferences = 
                    actionRequest.getPreferences();         
    preferences.setValue("maxBooksLimit", maxBooksLimit);
    preferences.store();
    
    actionResponse.setPortletMode(PortletMode.VIEW);
}

}
